<?php
/**
 * Created by PhpStorm.
 * User: huangj11
 * Date: 2021/1/14
 * Time: 19:36
 */

namespace business\elasticsearch\repositories;


use business\elasticsearch\models\SupplierWordsModel;
use yii\elasticsearch\Command;

class SupplierWordsRepository
{
    /**
     * 创建索引
     * @return mixed
     */
    public function createIndex()
    {
        return SupplierWordsModel::getDb()
            ->createCommand()
            ->createIndex(
                SupplierWordsModel::index(),
                [
                    'settings' => [
                        'index' => [
                            'number_of_shards' => 5,
                            'number_of_replicas' => 1,
                            'analysis.analyzer.default.type' => 'ik_max_word',
                        ],
                    ],
                    'mappings' => [
                        SupplierWordsModel::type() => [
                            'properties' => [
                                'uid' => [
                                    'type' => 'long',
                                ],
                                'word' => [
                                    'type' => 'text',
                                    "fields" => [
                                        "keyword" => [
                                            "type" => "keyword",
                                            "ignore_above" => 256
                                        ]
                                    ]
                                ],
                                'type' => [
                                    'type' => 'long',
                                ],
                                'indexCreatedTime' => [
                                    'type' => 'date',
                                    'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis'
                                ],
                            ]
                        ]
                    ]
                ]
            );
    }

    /**
     * 索引是否存在
     * @return mixed
     */
    public function existsIndex()
    {
        return SupplierWordsModel::getDb()
            ->createCommand()
            ->indexExists(SupplierWordsModel::index());
    }

    /**
     * 保存记录
     * @param array $data
     * @param int $id
     * @return array|mixed
     */
    public function saveRecord($data = [], $id = 0)
    {
        if (!empty($data)) {
            return SupplierWordsModel::getDb()
                ->createCommand()
                ->insert(
                    SupplierWordsModel::index()
                    , SupplierWordsModel::type()
                    , $data
                    , $id
                );
        }

        return [];
    }

    /**
     * 更新记录
     * @param $id
     * @param array $data
     * @return mixed
     */
    public function updateRecord($id, $data = [])
    {
        return SupplierWordsModel::getDb()
            ->createCommand()
            ->update(
                SupplierWordsModel::index()
                , SupplierWordsModel::type()
                , $id
                , $data
            );
    }

    /**
     * 删除记录
     * @param $id
     * @return mixed
     */
    public function deleteRecord($id)
    {
        if (!empty($id)) {
            return SupplierWordsModel::getDb()
                ->createCommand()
                ->delete(
                    SupplierWordsModel::index()
                    , SupplierWordsModel::type()
                    , $id
                );
        }
        return true;
    }

}